package com.acwing.lc.part1;

/**
 * @author rkc
 * @date 2022/9/17 10:44
 */
public class LC66加一 {

    public int[] plusOne(int[] digits) {
        reverse(digits);
        digits[0]++;
        int t = 0;
        for (int i = 0; i < digits.length; i++) {
            t += digits[i];
            digits[i] = t % 10;
            t /= 10;
        }
        if (t != 0) {
            int[] ans = new int[digits.length + 1];
            System.arraycopy(digits, 0, ans, 0, digits.length);
            ans[digits.length] = t;
            reverse(ans);
            return ans;
        }
        reverse(digits);
        return digits;
    }

    private void reverse(int[] arr) {
        for (int l = 0, r = arr.length - 1; l < r; l++, r--) {
            int t = arr[l];
            arr[l] = arr[r];
            arr[r] = t;
        }
    }
}
